package org.zfes.snowier.zbss.xzy.biz.solution.service.impl;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZObjectUtil;
import org.zfes.snowier.core.util.ZStrUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.core.data.ParamMap;
import com.google.common.collect.Lists;
import org.zfes.snowier.core.data.SelectVo;
import org.zfes.snowier.zbss.xzy.biz.solution.dao.SolutionMapper;
import org.zfes.snowier.zbss.xzy.biz.solution.model.Solution;
import org.zfes.snowier.zbss.xzy.biz.solution.service.IAlgorithmService;
import org.zfes.snowier.zbss.xzy.biz.solution.service.ISolutionAlgorithmService;
import org.zfes.snowier.zbss.xzy.biz.solution.service.ISolutionService;

import java.util.stream.Stream;
@Service
public class SolutionServiceImpl implements ISolutionService{
 	@Autowired
	private SolutionMapper solutionMapper;
 	@Autowired
 	private IAlgorithmService algorithmService;
 	@Autowired
 	private  ISolutionAlgorithmService solutionAlgorithmService;
 	//loadBySolutionId
 	
 	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createSolution(Solution solution) {
		ZBeanUtil.setCreateInfo(solution);
		ZBeanUtil.validateBean(solution);
		solutionMapper.insertSelective(solution);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<Solution> loadSolutionById(Long id) {
		if(id!=null){
			return Optional.ofNullable(solutionMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateSolution(Solution solution) {
		ZBeanUtil.setUpdatedInfo(solution);
		ZBeanUtil.validateBean(solution);
		solutionMapper.updateByPrimaryKeySelective(solution);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteSolution(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		solutionMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setSolutionStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		solutionMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadSolutionDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		pm.getObj("name").ifPresent(v->pm.updateParam("name", "%"+v+"%"));
		DataSet ds=DataSet.newDS2(solutionMapper.selectListCount(pm), solutionMapper.selectMapListPage(pm));
		return ds;
	}
	
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<SelectVo> loadSolutionSelectVo(String keywords) {
		if(ZStrUtil.hasText(keywords)) {
			
		}
		List<SelectVo> voList=solutionMapper.selectDropList(Byte.valueOf("1"));
		
		return voList==null?Lists.newArrayList():voList;
	}
	

	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<Solution> loadByIdsNotIn(List<Long> ids,String nameOrCode){

		String nameOrCodeStr ="";
		
		if(ZStrUtil.hasText(nameOrCode)) {
			nameOrCodeStr = "%"+nameOrCode+"%";
		}
		return solutionMapper.selectByIdsNotIn(ids,nameOrCodeStr);
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<Solution> loadByIds(List<Long> ids){
		if(ZObjectUtil.isEmpty(ids)) {
			return Lists.newArrayList();
		}
		return solutionMapper.selectByIds(ids);
	}
	
	
    
}